<template>
  <!-- 理想的注册页面 -->
  <div class="container">
    <div class='word'>
      <!-- <img src='../assets/img/word.png'> -->
    </div>
    <div class="main">
      <!-- <div class='avater_box'>
        <img src='../assets/photo.jpg'>
      </div> -->
      <div class="head">
        <div v-bind:class="flag1" @click="pclick">个人登录</div>
        <div v-bind:class="flag2" @click="eclick">账户登录</div>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 账户登录 -->
        <div v-show="!show" class="emailIn">
          <el-form-item label="用户名" prop="account">
            <el-input
              placeholder="请输入用户名"
              type="text"
              v-model="ruleForm.account"
              autocomplete="off"
              @input="inputLimit"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>
        <!-- 邮箱登录 -->
        <div v-show="show">
          <el-form-item label="邮箱" prop="email">
            <el-input
              placeholder="请输入邮箱"
              v-model="ruleForm.email"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <div class="email">
            <el-form-item label="验证码" prop="checkEmail">
              <el-input
                type="text"
                v-model="ruleForm.checkEmail"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <button
              class="send"
              v-if="showbtn"
              type="button"
              @click="sendEmail"
            >
              发送验证码
            </button>
            <button class="send red" v-if="!showbtn" type="button">
              {{ sec }}
            </button>
          </div>
        </div>

        <div class="bottom">
          <div>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </div>
          <div class="forget">
            <p class="word"  @click='toRegister'>没有账号?立即注册</p>
            <p class="word"  @click="toforgePwd">忘记密码</p>
          </div>
        </div>
      </el-form>

      <!-- <div v-show="tipMessage.length > 0" style="color: red">
        <p class="tip">{{ tipMessage }}</p>
      </div> -->
    </div>
  </div>
</template>
<script>
export default {
  data() {
    //判断函数
    var validateAccount = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不能为空"));
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("邮箱不能为空"));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      if (value.length < 6) {
        callback(new Error("密码长度不得小于6位"));
      }
    };
    var validateCheckEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      }
    };
    //   var validatePass2 = (rule, value, callback) => {
    //     if (value === '') {
    //       callback(new Error('请再次输入密码'));
    //     } else if (value !== this.ruleForm.pass) {
    //       callback(new Error('两次输入密码不一致!'));
    //     } else {
    //       callback();
    //     }
    //   };
    return {
      // selfstyle: {
      //   width: "350px",
      // },
      flag1: "accountLog",
      flag2: "emailLog",
      show: false,
      sec: "",
      //发回的验证码
      code: "",
      showbtn: true,
      tipMessage: "",
      ruleForm: {
        pass: "",
        account: "",
        email: "",
        checkEmail: "",
        panduan: true,
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        //   checkPass: [
        //     { validator: validatePass2, trigger: 'blur' }
        //   ],
        account: [{ validator: validateAccount, trigger: "blur" }],
        email: [{ validator: validateEmail, trigger: "blur" }],
        checkEmail: [{ validator: validateCheckEmail, trigger: "blur" }],
      },
    };
  },
  methods: {
    pclick() {
      this.show = false;
      this.flag2 = "emailLog";
      this.flag1 = "accountLog";
    },
    eclick() {
      this.show = true;
      this.flag1 = "accountLogNew";
      this.flag2 = "emailLogNew";
    },
    toforgePwd() {
      this.$router.push("/changePwd");
    },
    toRegister() {
      this.$router.push("/register");
    },
    //判断是否为中文
    inputLimit(val) {
      // 正则匹配非中英文及数字的字符
      let reg = /[^\u4e00-\u9fa5a-zA-Z0-9]/;
      // 查找是否有非中英文及数字的字符
      // 没有则返回-1，有则返回对应位置
      let n = val.search(reg);
      // 由于每次触发，通常只有最后一个字符是有可能异常的
      // 所以这里是直接截取异常字符前面的部分
      if (n !== -1) {
        this.ruleForm.account = val.slice(0, n);
      }
      console.log(n);
    },
    //提交设置
    async submitForm(formName) {
      //ref为标志符，以下是表单验证
      this.$router.push("/home")
      if (this.checkemail != this.code) {
        this.$message({
          message: "验证码错误",
          type: "error",
        });
        return;
      }

      this.$refs[formName].validate((valid) => {
        console.log(valid);
        if (valid) {
          //个人提交
          if (this.flag1 == "accountLog") {
            const { data: res } = this.$http.post("http1", this.formForm);
            res.meta.status = 200;
            if (res.meta.status != 200)
              this.$message({
                message: "登录失败",
                type: "error",
              });
            else
              this.$message({
                message: "登录成功",
                type: "success",
              });
            console.log("accountLog");
          } else {
            console.log("emailLog");
            const { data: res } = this.$http.post("http", this.formForm);
            if (res.meta.status != 200)
              this.$message({
                message: "登录失败",
                type: "error",
              });
            else {
              this.$message({
                message: "登录成功",
                type: "success",
              });
              window.location.href = "/home";
            }
          }
          console.log("提交成功!!");

          // window.sessionStorage.setItem("token", res.data.token)
        } else {
          console.log("提交失败!!");
          return false;
        }
      });
    },
    async sendEmail() {
      try {
        const res = await this.$axios.post(`http`, {
          action: "email",
          email: this.ruleForm.email,
        });
        const info = res.data;
        console.log(info);
        //显示验证码
        if (info.num != null) {
          this.code = info.num;
          this.showbtn = false;
          //时间间隔
          this.sec = 300;
          setInterval(() => {
            this.sec = this.sec - 1;
            if (this.sec == 0) {
              this.showbtn = true;
            }
          }, 1000);
        } else {
          this.$message({
            message: "验证码发送失败",
            type: "error",
            offset: 70,
          });
        }
      } catch (err) {
        this.$message({
          message: "发送失败,请刷新页面重试！",
          type: "error",
          offset: 70,
        });
        console.log(err);
      }
    },
    //重置设置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang='less' scope>
.container {
  // background-color: #2b4b6b;
  // position: fixed;
  // top: 0;
  // left: 0;
  // background-position: center 0;
  // background-repeat: no-repeat;
  // background-attachment: fixed;
  // background-size: cover;
  // -webkit-background-size: cover;
  // -o-background-size: cover;
  // /* // 用浮点数来定义缩放比例。不允许负值 */
  // zoom: 1;
  // width: 100%;
  // background-image: url('../assets/img/bg.jpg');
    background: linear-gradient(to bottom, rgba(158,178,210), rgba(76,108,154,1)); /* 标准的语法 */
  height: 100%;
  .word{
    display:flex;
    width:100%;
    img{
      margin-right:0;
    }
  }

  .main {
    width: 500px;
    height: auto;
    display: inline-block;
    margin-right: 10px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 15px 25px rgba(0, 0, 0, 0.5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    border-radius: 10px 10px 10px 10px;
    /*边框圆角，四个角均为15px*/
    .head {
      width: 500px;
      height: 100px;
      background: rgb(247, 249, 250);
      // background:#dddfe6;
      display: flex;

      text-align: center;
      font-weight: bold;
      font-size: 20px;
      border-radius: 10px 10px 0px 0px;
      // 原始状态，带有下划线
      .accountLog {
        width: 250px;
        height: 100px;
        // background:red;
        line-height: 100px;
        padding-bottom: 8px;
        margin-left: 40px;
        color: rgb(64, 128, 255);

        font-size: 22px;
      }
      .accountLogNew:hover {
        text-decoration: underline;
      }
      .accountLogNew {
        width: 250px;
        height: 100px;
        // background:red;
        padding-bottom: 8px;
        margin-left: 40px;
        line-height: 100px;
        cursor: pointer;
      }
      .emailLog:hover {
        text-decoration: underline;
      }
      .emailLog {
        width: 250px;
        height: 100px;
        padding-bottom: 8px;
        margin-right: 40px;
        line-height: 100px;
        cursor: pointer;
      }
      .emailLogNew {
        width: 250px;
        height: 100px;
        // background:red;
        line-height: 100px;
        padding-bottom: 8px;
        margin-right: 40px;
        color: rgb(64, 128, 255);
        font-size: 22px;
      }
    }
    .email {
      display: flex;
      flex-direction: row;
      justify-content: space-between;

      .send {
        height: 40px;
        width: 100px;
        margin: 5px;
        margin-right: 10px;
        border: none;
        outline: none;
        font-size: 8px;
        color: #fff;
        background: rgb(64, 158, 255);
        padding: 10px 10px;
        /* test-align: center; */
        cursor: pointer;
        /*光标呈现为指示链接的指针（一只手）*/
        border-radius: 10px;
      }
      .red {
        cursor: none;
        background-color: rgba(255, 0, 0, 0.5);
      }
    }
    .bottom {
      display: flex;
      flex-direction: row;
      justify-content: space-between;

      .forget {
        font-size: 14px;
        color: #dcddd8;
        //文字位于底部
        // position:relative
        .word {
          // bottom: 0px;
          // padding: 0px;
          // margin-right: 40px;
          cursor: pointer;
          // color: rgb(46, 158, 255);
          // text-decoration: underline;
          // bottom: 0px;
          // padding: 0px;
          // margin:0;
          margin-right: 40px;
          color: #dcddd8;
          //  position: absolute;
        }
        .word:hover {
          // position: relative;
          // bottom: 0px;
          // padding: 0px;
          margin-right: 40px;
          cursor: pointer;
          color: rgb(46, 158, 255);
          text-decoration: underline;
        }
      }
    }
  }
}
</style>
